package com.controleFamiliar.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class DataSource implements InterfaceDataSource{
	 
    private String url;
    private String driver;
    private String usuario;
    private String senha;
    
    private ResourceBundle config;
    
    public DataSource() throws ClassNotFoundException{
        config = ResourceBundle.getBundle("com.controleFamiliar.properties.Banco"); //pacote onde está o arquivo properties
        this.url = config.getString("url");
        this.driver = config.getString("driver");
        this.usuario = config.getString("usuario");
        this.senha = config.getString("senha");
        
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new ClassNotFoundException("DataSource : ocorreu um problema ao carregar o driver");
        }
    }
 
    @Override
    public Connection getConnection() throws SQLException{
        try {
            return DriverManager.getConnection(url, usuario, senha);
        } catch (SQLException e) {
            throw new SQLException("Ocorreu um erro ao estabelecer conexão com o banco de dados");
        }
    }
}